//
//  CouponTableViewCell.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/22.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "CouponTableViewCell.h"


@interface CouponTableViewCell ()

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, strong)UILabel * countLabel;
@property (nonatomic, strong)UILabel * moneyLabel;
@property (nonatomic, strong)UILabel * symbolLabel;
@property (nonatomic, strong)UILabel * conditionLabel;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * timeLabel;

@end

#define eatColor ColorWithRGB(0xF0614C)
#define playColor ColorWithRGB(0xA45AF5)
#define sportsColor ColorWithRGB(0x16D37D)
#define travleColor ColorWithRGB(0x3884EB)
#define allColor ColorWithRGB(0xDAC901)

@implementation CouponTableViewCell


- (void)setModel:(CouponModel *)model{
    
    _model = model;
    
    int usable_goods_type = [model.usable_goods_category_id intValue];
    if (usable_goods_type == 0) {
        
        self.nameLabel.textColor = allColor;
        self.conditionLabel.textColor = allColor;
        self.countLabel.textColor = allColor;
        self.timeLabel.textColor = allColor;
        self.symbolLabel.textColor = allColor;
        self.moneyLabel.textColor = allColor;
        self.editLabel.textColor = allColor;
        self.backImageView.image = [UIImage imageNamed:@"新通用"];
    }else if (usable_goods_type == 1137){
        
        self.nameLabel.textColor = eatColor;
        self.conditionLabel.textColor = eatColor;
        self.countLabel.textColor = eatColor;
        self.timeLabel.textColor = eatColor;
        self.symbolLabel.textColor = eatColor;
        self.moneyLabel.textColor = eatColor;
        self.editLabel.textColor = eatColor;
        self.backImageView.image = [UIImage imageNamed:@"吃喝背景"];
    }else if (usable_goods_type == 1138){
        
        self.nameLabel.textColor = playColor;
        self.conditionLabel.textColor = playColor;
        self.countLabel.textColor = playColor;
        self.timeLabel.textColor = playColor;
        self.symbolLabel.textColor = playColor;
        self.moneyLabel.textColor = playColor;
        self.editLabel.textColor = playColor;
        self.backImageView.image = [UIImage imageNamed:@"玩乐"];
    }else if (usable_goods_type == 1139){
        
        self.nameLabel.textColor = sportsColor;
        self.conditionLabel.textColor = sportsColor;
        self.countLabel.textColor = sportsColor;
        self.timeLabel.textColor = sportsColor;
        self.symbolLabel.textColor = sportsColor;
        self.moneyLabel.textColor = sportsColor;
        self.editLabel.textColor = sportsColor;
        self.backImageView.image = [UIImage imageNamed:@"运动"];
    }else if (usable_goods_type == 1140){
        
        self.nameLabel.textColor = travleColor;
        self.conditionLabel.textColor = travleColor;
        self.countLabel.textColor = travleColor;
        self.timeLabel.textColor = travleColor;
        self.symbolLabel.textColor = travleColor;
        self.moneyLabel.textColor = travleColor;
        self.editLabel.textColor = travleColor;
        self.backImageView.image = [UIImage imageNamed:@"旅行"];
    }
    
    //    int is_over = [model.is_over intValue];
    //
    //    if (is_over == 1) {
    //
    //        self.nameLabel.textColor = overColor;
    //        self.conditionLabel.textColor = overColor;
    //        self.countLabel.textColor = overColor;
    //        self.timeLabel.textColor = overColor;
    //        self.symbolLabel.textColor = overColor;
    //        self.moneyLabel.textColor = overColor;
    //        self.editLabel.textColor = overColor;
    //        self.backImageView.image = [UIImage imageNamed:@"过期"];
    //    }
    
    self.moneyLabel.text = [NSString stringWithFormat:@"%@",model.discount];
    int type = [model.type intValue];
    
    if (type == 1) {
        
        self.symbolLabel.text = @"元";
        
//        [self.symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.mas_equalTo(self.contentView);
//            make.leading.mas_equalTo(self.contentView).offset(190 * SCREENH6);
//        }];
//
//        [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.leading.mas_equalTo(self.symbolLabel.mas_trailing);
//            make.centerY.mas_equalTo(self.symbolLabel);
//        }];
        
    }else{
        
        self.symbolLabel.text = @"折";
        
        
    }
    
    [self.symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-110 * SCREENH6);
        make.centerY.mas_equalTo(self.moneyLabel);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.trailing.mas_equalTo(self.symbolLabel.mas_leading);
    }];
    
    self.nameLabel.text = model.coupons_name;
    self.conditionLabel.text = [NSString stringWithFormat:@"·满%@使用",model.use_standard];
    self.countLabel.text = [NSString stringWithFormat:@"·总量:%@ 已领取:%@",model.issue_number,model.user_get_number];
    self.timeLabel.text = [NSString stringWithFormat:@"使用期限:%@-%@",model.starttime,model.overtime];
    
    if (model.is_receive) {
        
        int is_receive = [model.is_receive intValue];
        if (is_receive == 0) {
            
            self.editLabel.text = @"领取";
        }else{
            
            self.editLabel.text = @"已领取";
        }
    }
}


- (void)setUserModel:(SureUserCouponModel *)userModel{
    
    _userModel = userModel;
    
    int usable_goods_type = [userModel.merchants_coupons_usable_goods_category_id intValue];
    if (usable_goods_type == 0) {
        
        self.nameLabel.textColor = allColor;
        self.conditionLabel.textColor = allColor;
        self.countLabel.textColor = allColor;
        self.timeLabel.textColor = allColor;
        self.symbolLabel.textColor = allColor;
        self.moneyLabel.textColor = allColor;
        self.editLabel.textColor = allColor;
        self.backImageView.image = [UIImage imageNamed:@"新通用"];
    }else if (usable_goods_type == 1137){
        
        self.nameLabel.textColor = eatColor;
        self.conditionLabel.textColor = eatColor;
        self.countLabel.textColor = eatColor;
        self.timeLabel.textColor = eatColor;
        self.symbolLabel.textColor = eatColor;
        self.moneyLabel.textColor = eatColor;
        self.editLabel.textColor = eatColor;
        self.backImageView.image = [UIImage imageNamed:@"吃喝背景"];
    }else if (usable_goods_type == 1138){
        
        self.nameLabel.textColor = playColor;
        self.conditionLabel.textColor = playColor;
        self.countLabel.textColor = playColor;
        self.timeLabel.textColor = playColor;
        self.symbolLabel.textColor = playColor;
        self.moneyLabel.textColor = playColor;
        self.editLabel.textColor = playColor;
        self.backImageView.image = [UIImage imageNamed:@"玩乐"];
    }else if (usable_goods_type == 1139){
        
        self.nameLabel.textColor = sportsColor;
        self.conditionLabel.textColor = sportsColor;
        self.countLabel.textColor = sportsColor;
        self.timeLabel.textColor = sportsColor;
        self.symbolLabel.textColor = sportsColor;
        self.moneyLabel.textColor = sportsColor;
        self.editLabel.textColor = sportsColor;
        self.backImageView.image = [UIImage imageNamed:@"运动"];
    }else if (usable_goods_type == 1140){
        
        self.nameLabel.textColor = travleColor;
        self.conditionLabel.textColor = travleColor;
        self.countLabel.textColor = travleColor;
        self.timeLabel.textColor = travleColor;
        self.symbolLabel.textColor = travleColor;
        self.moneyLabel.textColor = travleColor;
        self.editLabel.textColor = travleColor;
        self.backImageView.image = [UIImage imageNamed:@"旅行"];
    }
    
    self.moneyLabel.text = [NSString stringWithFormat:@"%@",userModel.merchants_coupons_discount];
//    CGSize size = [self preferredSizeWithMaxWidth:150 * SCREENH6 withLabel:self.moneyLabel];
    int type = [userModel.merchants_coupons_type intValue];
    
    if (type == 1) {
        
        self.symbolLabel.text = @"元";
        ////        NSLog(@"===============%@",self.moneyLabel.text);
        //        [self.symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.centerY.mas_equalTo(self.contentView);
        //            make.leading.mas_equalTo(self.contentView).offset(170 * SCREENH6);
        //        }];
        //
        //        [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.centerY.mas_equalTo(self.symbolLabel);
        //            make.leading.mas_equalTo(self.symbolLabel.mas_trailing);
        //        }];
//        self.symbolLabel.frame = CGRectMake(170 * SCREENH6, 35 * SCREENH6, 13 * SCREENH6, 13 * SCREENH6);
//        self.moneyLabel.frame = CGRectMake(180 * SCREENH6, 30 * SCREENH6, size.width, 22 * SCREENH6);
    }else if (type == 2){
        
        self.symbolLabel.text = @"折";
        
        //        [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.centerY.mas_equalTo(self.contentView);
        //            make.leading.mas_equalTo(self.contentView).offset(175 * SCREENH6);
        //        }];
        //
        //        [self.symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.leading.mas_equalTo(self.moneyLabel.mas_trailing);
        //            make.centerY.mas_equalTo(self.moneyLabel);
        //        }];
//        self.moneyLabel.frame = CGRectMake(185 * SCREENH6, 30 * SCREENH6, size.width, 22 * SCREENH6);
//
//        self.symbolLabel.frame = CGRectMake(185 * SCREENH6 + size.width, 35 * SCREENH6, 13 * SCREENH6, 13 * SCREENH6);
        
    }
    [self.symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-110 * SCREENH6);
        make.centerY.mas_equalTo(self.moneyLabel);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.trailing.mas_equalTo(self.symbolLabel.mas_leading);
    }];
    
    
    
    self.nameLabel.text = userModel.coupons_name;
    self.conditionLabel.text = [NSString stringWithFormat:@"·满%@使用",userModel.merchants_coupons_use_standard];
    self.countLabel.text = [NSString stringWithFormat:@"·%@",userModel.merchants_name];
    self.timeLabel.text = [NSString stringWithFormat:@"使用期限:%@-%@",userModel.merchants_coupons_starttime,userModel.merchants_coupons_overtime];
    self.editLabel.text = @"使用";
}


//- (void)setType:(NSString *)type{
//
//    _type = type;
//
//    if ([type isEqualToString:@"goods"]) {
//
//        self.editLabel.text = @"领取";
//    }
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = [UIImage imageNamed:@"通用"];
    self.backImageView = backImageView;
    
    [self.contentView addSubview:self.backImageView];
    
    self.nameLabel = [self setupLabelWithTitle:@"优惠券名字"];
    self.nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [self.backImageView addSubview:self.nameLabel];
    self.conditionLabel = [self setupLabelWithTitle:@"·满50使用"];
    [self.backImageView addSubview:self.conditionLabel];
    self.countLabel = [self setupLabelWithTitle:@"·发放数量:100 用户已领取:20"];
    [self.backImageView addSubview:self.countLabel];
    self.timeLabel = [self setupLabelWithTitle:@"使用期限:2018.01.16-2018.01.05"];
    self.timeLabel.font = [UIFont systemFontOfSize:8 * SCREENH6];
    [self.backImageView addSubview:self.timeLabel];
    self.editLabel = [self setupLabelWithTitle:@"编辑"];
    if ([self.type isEqualToString:@"goods"]) {
        
        self.editLabel.text = @"领取";
    }
    self.editLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [self.backImageView addSubview:self.editLabel];
    self.symbolLabel = [self setupLabelWithTitle:@"￥"];
    self.symbolLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    self.symbolLabel.textAlignment = NSTextAlignmentRight;
    [self.backImageView addSubview:self.symbolLabel];
    self.moneyLabel = [self setupLabelWithTitle:@"5"];
    self.moneyLabel.font = [UIFont systemFontOfSize:22 * SCREENH6];
    [self.backImageView addSubview:self.moneyLabel];
}


- (void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(97 * SCREENH6);
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.width.mas_equalTo(310 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(25 * SCREENH6);
        make.top.mas_equalTo(self.backImageView).offset(17 * SCREENH6);
    }];
    
    [self.conditionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.conditionLabel);
        make.top.mas_equalTo(self.conditionLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.countLabel);
        make.top.mas_equalTo(self.countLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.editLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backImageView);
        make.leading.mas_equalTo(self.backImageView).offset(250 * SCREENH6);
    }];
}


- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = allColor;
    label.font = [UIFont systemFontOfSize:10 * SCREENH6];
    label.text = title;
    
    return label;
}

- (CGSize)preferredSizeWithMaxWidth:(CGFloat)maxWidth withLabel:(UILabel *)label
{
    
    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, 10000000.0f)];
    size.width = fmin(size.width, maxWidth); //在numberOfLine为1模式下返回的可能会比maxWidth大，所以这里我们限制下
    return size;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
